home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / gs241j11.zip / ZKFZEIT.C < prev   
C/C++ Source or Header  |  1992-06-17  |  13KB  |  631 lines

  1. /*
  2.  * zkfzeit.c --- Draw Fonts with Zeit(TM) Vector Font Set
  3.  *
  4.  * Aug.5, 1991 Programmed by N.Katayama (katayama.nacsis.ac.jp)
  5.  * Mar.4, 1992 modifyed   by K-ras
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "math_.h"
  11. #include "memory_.h"
  12. #include "ghost.h"
  13. #include "oper.h"
  14. #include "errors.h"
  15. #include "gspath.h"
  16. #include "gsmatrix.h"
  17. #include "gscoord.h"
  18. #include "gsstate.h"
  19. #include "state.h"
  20. #ifdef X68000
  21. #include <iocslib.h>
  22. #endif
  23.  
  24. #define D_X_CL    500    /* horizontal center of data */
  25. #define D_Y_CL    472    /* vertical center of data */
  26. #define    D_Y_BL    872    /* base line of data */
  27.  
  28. #define B_X_CL    500    /* horizontal center in BuildChar */
  29. #define B_Y_CL    400    /* vertical center in BuildChar */
  30. #define B_Y_BL    0    /* base line in BuildChar */
  31.  
  32. /* Imported procedure */
  33.  
  34. extern int kf_is_vchar(P1(int));
  35. extern int kf_vmatrix(P5(int, floatp, floatp, gs_rect *, gs_matrix *));
  36.  
  37. /* Forward declaration */
  38.  
  39. private floatp map_x(P1(double));
  40. private floatp map_y(P1(double));
  41. private int draw_font(P2(char *, int));
  42.  
  43. /*
  44.  * zkfzeit
  45.  */
  46.  
  47. int
  48. zkfzeit(register os_ptr op)
  49. {
  50.     char *buffer;
  51.     int code, len, jis_code, wmode;
  52.  
  53.     check_type(op[-2], t_integer);        /* JIS Code */
  54.     check_type(op[-1], t_integer);        /* WMode */
  55.     check_type(op[0], t_string);        /* File Name */
  56.  
  57.     len = r_size(op);
  58.     if((buffer = gs_malloc(len + 1, 1, "zkfzeit")) == 0)
  59.         return e_VMerror;
  60.     memcpy(buffer, (char *)op->value.bytes, len);
  61.     buffer[len] = 0;
  62.  
  63.     jis_code = op[-2].value.intval;
  64.     wmode = op[-1].value.intval;
  65.  
  66.     if(wmode && kf_is_vchar(jis_code)) {
  67.         /* vertical kanji character */
  68.         gs_matrix smat, vmat;
  69.         gs_rect bbox;
  70.  
  71.         if((code = gs_gsave(igs)) < 0)
  72.             return code;
  73.         if((code = draw_font(buffer, jis_code)) < 0)
  74.             return code;
  75.         if((code = gs_flattenpath(igs)) < 0)
  76.             return code;
  77.         if((code = gs_pathbbox(igs, &bbox)) < 0)
  78.             return code;
  79.         if((code = gs_grestore(igs)) < 0)
  80.             return code;
  81.  
  82.         gs_currentmatrix(igs, &smat);
  83.         kf_vmatrix(jis_code, 
  84.                (floatp)B_X_CL, (floatp)B_Y_CL, &bbox, &vmat);
  85.         if((code = gs_concat(igs, &vmat)) < 0)
  86.             return code;
  87.         if((code = draw_font(buffer, jis_code)) < 0)
  88.             return code;
  89.         gs_setmatrix(igs, &smat);
  90.     }
  91.     else {
  92.         if((code = draw_font(buffer, jis_code)) < 0)
  93.             return code;
  94.     }
  95.  
  96.     pop(3);
  97.     gs_free(buffer, len + 1, 1, "zkfzeit");
  98.     return 0;
  99. }
  100.  
  101. /* -------- Initialization procedure -------- */
  102.  
  103. op_def zkfzeit_op_defs[] = {
  104.     {"3kfzeit", zkfzeit},
  105.     op_def_end(0)
  106. };
  107.  
  108. /* -------- Internal routines -------- */
  109.  
  110. /*
  111.  * Mapping from coordinates of data to coordinates in BuildChar
  112.  */
  113.  
  114. private floatp
  115. map_x(double x)
  116. {
  117.     static double factor = 
  118.         (double)(B_Y_CL - B_Y_BL) / (double)(D_Y_CL - D_Y_BL);
  119.  
  120.     return B_X_CL + (x - D_X_CL) * (factor > 0 ? factor : -factor);
  121. }
  122.  
  123. private floatp
  124. map_y(double y)
  125. {
  126.     static double factor = 
  127.         (double)(B_Y_CL - B_Y_BL) / (double)(D_Y_CL - D_Y_BL);
  128.  
  129.     return B_Y_CL + (y - D_Y_CL) * factor;
  130. }
  131.  
  132. /*------------------------------------------------------------------------*/
  133. /*------------ The part below is zeit.h written by T.Kawamoto ------------*/
  134. /*------------------------------------------------------------------------*/
  135.  
  136. /*
  137.  *    zeit font file library Ver 1.00
  138.  *        for Human68k
  139.  *    Copyright 1991 by Ext(T.Kawamoto)
  140.  *
  141.  *    zeit.h
  142.  */
  143.  
  144. #include <stdio.h>
  145. #ifdef X68000
  146. #include <stdlib.h>
  147. #include <doslib.h>
  148. #endif
  149.  
  150. #define Zerror_no_memory    (-100)
  151. #define Zerror_cannot_read  (-101)
  152. #define Zerror_cannot_write (-102)
  153. #define Zerror_end_of_file  (-103)
  154. #define Zerror_no_font      (-104)
  155.  
  156. typedef struct
  157. {
  158.   int x, y;
  159. } Point;
  160.  
  161. typedef struct CYCLE
  162. {
  163.   struct CYCLE *next;
  164.   Point point;
  165. } Cycle;
  166.  
  167. typedef struct FONT
  168. {
  169.   struct FONT *next;
  170.   Cycle *cycle;
  171. } Font;
  172.  
  173. typedef union FREE
  174. {
  175.   union FREE *next;
  176.   Cycle cycle;
  177.   Font font;
  178. } Free;
  179.  
  180. #define CODE_SIZE 4418
  181.  
  182. typedef struct
  183. {
  184.   unsigned char dummy[2];
  185.   unsigned long offsets[CODE_SIZE];
  186. } Zs_Header;
  187.  
  188. #define Zs_Header_SIZE (2 + (CODE_SIZE) * 4)
  189.  
  190. /*-----------------------------------------------------------------------*/
  191. /*---------------------------- end of zeit.h ----------------------------*/
  192. /*-----------------------------------------------------------------------*/
  193.  
  194. /***************************************************
  195.  * Zeit Font Accsess Routines
  196.  *     Original by Ext(T.Kawamoto)
  197.  *     modifyed by K-ras
  198.  **************************************************/
  199. private void  Zdraw_cycle(P1(Cycle *));
  200. private long  Zread_long(P1(FILE *));
  201. private int   Zread_10_bits(P1(FILE *));
  202. private int   Zread_header(P2(FILE *, Zs_Header *));
  203. private Cycle *Zread_cycle(P1(FILE *));
  204. private Font  *Zread_font();
  205. private int   jis2c(P1(int));
  206. private Free  *Zalloc();
  207. private Cycle *Zalloc_cycle();
  208. private Font  *Zalloc_font();
  209. private void  Zfree(P1(Free *));
  210. private void  Zfree_font(P1(Font *));
  211.  
  212. /*
  213.  * Hash Index
  214.  */
  215.  
  216. typedef struct index_item_s {
  217.     struct index_item_s *next;
  218.     char      *str;
  219.         FILE      *fd1;
  220.         FILE      *fd2;
  221.         Zs_Header read_header1;
  222.         Zs_Header read_header2;
  223. } index_item;
  224. private index_item *hash_index[256];
  225.  
  226. /* Hash function */
  227.  
  228. private int 
  229. hash(unsigned char *str)
  230. {
  231.     unsigned char hash;
  232.  
  233.     for(hash = 0; *str != 0; str++)
  234.         hash ^= *str;
  235.  
  236.     return hash;
  237. }
  238.         
  239. /* store */
  240.  
  241. private int
  242. store(char *str, FILE *fd1, FILE *fd2, Zs_Header *hptr1, Zs_Header *hptr2)
  243. {
  244.     int key = hash((unsigned char *)str);
  245.     index_item *item;
  246.  
  247.     if((item = (index_item *)malloc(sizeof(index_item))) == 0)
  248.         return e_VMerror;
  249.     if((item->str = (char *)malloc(strlen(str) + 1)) == 0)
  250.         return e_VMerror;
  251.     strcpy(item->str, str);
  252.     item->next = hash_index[key];
  253.     item->fd1 = fd1;
  254.     item->fd2 = fd2;
  255.         memcpy(&(item->read_header1),hptr1,Zs_Header_SIZE);
  256.         memcpy(&(item->read_header2),hptr2,Zs_Header_SIZE);
  257.     hash_index[key] = item;
  258.     return 0;
  259. }
  260.  
  261. /* search */
  262.  
  263. private int
  264. search(char *str, index_item **iptr)
  265. {
  266.     int key = hash((unsigned char *)str);
  267.     index_item *item;
  268.  
  269.     item = hash_index[key];
  270.     while(item != 0) {
  271.         if(strcmp(item->str, str) == 0) {
  272.             *iptr = item;
  273.             return 1;
  274.         }
  275.         item = item->next;
  276.     }
  277.     return 0;
  278. }
  279.  
  280. /*
  281.  * Open font 
  282.  */
  283.  
  284. private int 
  285. open_font(char *file, index_item **iptr)
  286. {
  287.     int        code;
  288.         FILE        *wfd1,*wfd2;
  289.         index_item    *wptr;
  290.         Zs_Header    header1,header2;
  291.     char         fname[128];
  292.  
  293.         if(search(file, &wptr)){
  294.                 *iptr = wptr;
  295.         return 0;
  296.     } else {
  297.             strcpy(fname, file);
  298.             strcat(fname, ".vf1");
  299.         if((wfd1 = fopen(fname,"rb")) == NULL) {
  300.             perror(fname);
  301.             return -1;
  302.         }
  303.             Zread_header(wfd1,&header1);
  304.             strcpy(fname, file);
  305.             strcat(fname, ".vf2");
  306.         if((wfd2 = fopen(fname,"rb")) == NULL) {
  307.             perror(fname);
  308.             return -1;
  309.         }
  310.             Zread_header(wfd2,&header2);
  311.         if((code = store(file, wfd1, wfd2, &header1, &header2)) < 0)
  312.             return code;
  313.                 search(file, &wptr);
  314.                 *iptr = wptr;
  315.         return 0;
  316.     }
  317. }
  318.  
  319. /*
  320.  * moveto
  321.  */
  322. #define moveto(x, y)    gs_moveto(igs, map_x(x), map_y(y))
  323.  
  324. /*
  325.  * lineto
  326.  */
  327. #define lineto(x, y)    gs_lineto(igs, map_x(x), map_y(y))
  328.  
  329. /*
  330.  * Draw Font
  331.  */
  332. private int 
  333. draw_font(char *font_name, int jis)
  334. {
  335.         Font       *ptr, *fontptr;
  336.         index_item *iptr;
  337.         float      x,y;
  338.  
  339.     if(open_font(font_name, &iptr) != 0) {
  340.             return e_rangecheck;
  341.     }
  342.         if((fontptr = Zread_font(jis, iptr)) == NULL) {
  343.         return 0;
  344.         }
  345.  
  346.         for(ptr = fontptr; ptr; ptr = ptr->next){
  347.             x = (float)(ptr->cycle->point.x);
  348.             y = (float)(ptr->cycle->point.y);
  349.             moveto(x, y);
  350.             Zdraw_cycle(ptr->cycle);
  351.         }
  352.  
  353.         Zfree_font(fontptr);
  354.  
  355.         return 0;
  356. }
  357.  
  358. /***************************************************
  359.  * Zeit Font Accsess Routines
  360.  *     Original by Ext(T.Kawamoto)
  361.  *     modifyed by K-ras
  362.  **************************************************/
  363. /*
  364.  *    zeit font file library Ver 1.00
  365.  *        for Human68k
  366.  *    Copyright 1991 by Ext(T.Kawamoto)
  367.  */
  368. private void 
  369. Zdraw_cycle(Cycle *ptr)
  370. {
  371.     const Cycle *ptr0;
  372.     float x, y;
  373.  
  374.         ptr0 = ptr;
  375.         do {
  376.         x   = (float)(ptr->next->point.x);
  377.         y   = (float)(ptr->next->point.y);
  378.         lineto(x , y);
  379.         ptr = ptr->next;
  380.     } while (ptr != ptr0);
  381. }
  382.  
  383. private int 
  384. Zread_byte(FILE *fp)
  385. {
  386.         return fgetc(fp);
  387. }
  388.  
  389. private long
  390. Zread_long(FILE *fp)
  391. {
  392.     unsigned long dummy;
  393.  
  394.         dummy = Zread_byte(fp);
  395.         dummy |= (unsigned long)Zread_byte(fp) << 8;
  396.         dummy |= (unsigned long)Zread_byte(fp) << 16;
  397.         dummy |= (unsigned long)Zread_byte(fp) << 24;
  398.  
  399.         return dummy;
  400. }
  401.  
  402. /****************************************/
  403. /*                    */
  404. /*    seek, read 10 bits        */
  405. /*                    */
  406. /****************************************/
  407. static short left;
  408. static unsigned long divide;
  409.  
  410. private void 
  411. Zseek(FILE *fp, long offset)
  412. {
  413.         fseek(fp, offset, 0);
  414.         left       = 0;
  415.         divide = 0;
  416. }
  417.  
  418. private int 
  419. Zread_10_bits(FILE *fp)
  420. {
  421.         if (left < 10) {
  422.         divide <<= 16;
  423.             divide |= (unsigned long)Zread_byte(fp);
  424.             divide |= (unsigned long)Zread_byte(fp) << 8;
  425.         left   += 16;
  426.     }
  427.  
  428.         left -= 10;
  429.         return (divide >> left) & 0x3ff;
  430. }
  431.  
  432. /****************************************/
  433. /*                    */
  434. /* Zeit font header index read        */
  435. /*                    */
  436. /****************************************/
  437. private int 
  438. Zread_header(FILE *fp, Zs_Header *hptr)
  439. {
  440.     int i;
  441.  
  442.         Zseek(fp, 0);
  443.         hptr->dummy[0] = Zread_byte(fp);
  444.         hptr->dummy[1] = Zread_byte(fp);
  445.         for (i = 0; i < CODE_SIZE; ++i)
  446.         hptr->offsets[i] = Zread_long(fp);
  447.  
  448.         return 0;
  449. }
  450.  
  451. /****************************************/
  452. /*                    */
  453. /*    cycle, font            */
  454. /*                    */
  455. /****************************************/
  456. private Cycle * 
  457. Zread_cycle(FILE *fp)
  458. {
  459.     int   x0, y0, x, y;
  460.     Cycle *cptr0, *cptr;
  461.  
  462.         x0 = Zread_10_bits(fp);
  463.         y0 = Zread_10_bits(fp);
  464.         if (x0 == 1023 && y0 == 1023)
  465.         return NULL;
  466.         if ((cptr0 = cptr = Zalloc_cycle()) == NULL)
  467.         return cptr;
  468.         cptr->point.x = x0;
  469.         cptr->point.y = y0;
  470.         while (1) {
  471.         x = Zread_10_bits(fp);
  472.         y = Zread_10_bits(fp);
  473.         if (x == 1023 && y == 1023)
  474.         break;
  475.         if ((cptr->next = Zalloc_cycle()) == NULL)
  476.         return cptr->next;
  477.         cptr          = cptr->next;
  478.         cptr->point.x = x;
  479.         cptr->point.y = y;
  480.     }
  481.         if (cptr->point.x == cptr0->point.x && cptr->point.y == cptr0->point.y) {
  482.         cptr->next = cptr0->next;
  483.         Zfree((Free *)cptr0);
  484.         return cptr;
  485.     } else {
  486.         cptr->next = cptr0;
  487.         return cptr0;
  488.     }
  489. }
  490.  
  491. private Font * 
  492. Zread_font(int code, index_item *iptr)
  493. {
  494.     Font  font, *fptr;
  495.     Cycle *cptr;
  496.     int   code_offset;
  497.     long  seek_offset, no_font_flag;
  498.     FILE  *fp;
  499.  
  500.         code_offset = jis2c(code);
  501.         if (code < 0x5000) {
  502.         fp = iptr->fd1;
  503.         seek_offset  = no_font_flag = iptr->read_header1.offsets[code_offset];
  504.         seek_offset += Zs_Header_SIZE;
  505.     } else {
  506.         fp = iptr->fd2;
  507.         seek_offset  = no_font_flag = iptr->read_header2.offsets[code_offset];
  508.         seek_offset += Zs_Header_SIZE;
  509.     }
  510.         if (code_offset == 0 || no_font_flag == 0xffffffff)
  511.         return NULL;
  512.  
  513.         Zseek(fp, seek_offset);
  514.         fptr = &font;
  515.         while (1) {
  516.         cptr = Zread_cycle(fp);
  517.         if (cptr == NULL)
  518.         break;
  519.         if ((fptr->next = Zalloc_font()) == NULL)
  520.         return fptr->next;
  521.         fptr        = fptr->next;
  522.         fptr->cycle = cptr;
  523.     }
  524.         fptr->next = NULL;
  525.         return font.next;
  526. }
  527.  
  528. private int
  529. jis2c(int code)
  530. {
  531.     int  zscode;
  532.  
  533.         if ( code < 0x5000 ){
  534.         zscode = (((code >> 8) & 0xff) - 0x21) * 0x5e + (code & 0xff) - 0x21;
  535.     } else {
  536.         zscode = (((code >> 8) & 0xff) - 0x50) * 0x5e + (code & 0xff) - 0x21;
  537.     }
  538.  
  539.         return zscode;
  540. }
  541.  
  542. /****************************************/
  543. /*                    */
  544. /*    alloc, free            */
  545. /*                    */
  546. /****************************************/
  547. static Free *free_ptr = NULL;
  548.  
  549. private Free *
  550. Zalloc(void)
  551. {
  552.     Free *ptr;
  553.  
  554.         if (free_ptr == NULL) {
  555.         int i;
  556.  
  557.         if ((ptr = (Free *)malloc(sizeof(Free) * 1024)) == NULL) {
  558.         printf("\ngs: Zalloc malloc error\n");
  559.                 exit(0);
  560.             }
  561.             free_ptr = ptr;
  562.             for (i = 0; i < 1023; ++i, ++ptr)
  563.                 ptr->next = ptr + 1;
  564.             ptr->next = NULL;
  565.         }
  566.         ptr      = free_ptr;
  567.         free_ptr = free_ptr->next;
  568.  
  569.         return ptr;
  570. }
  571.  
  572. private Cycle * 
  573. Zalloc_cycle(void)
  574. {
  575.     Free *ptr;
  576.  
  577.         ptr = Zalloc();
  578.         if (ptr == NULL)
  579.         return (Cycle *)ptr;
  580.         return &(ptr->cycle);
  581. }
  582.  
  583. private Font * 
  584. Zalloc_font(void)
  585. {
  586.     Free *ptr;
  587.  
  588.         ptr = Zalloc();
  589.         if (ptr == NULL)
  590.         return (Font *)ptr;
  591.         return &(ptr->font);
  592. }
  593.  
  594. private void 
  595. Zfree(Free *ptr)
  596. {
  597.         if (ptr == NULL)
  598.         return;
  599.         ptr->next = free_ptr;
  600.         free_ptr  = ptr;
  601. }
  602.  
  603. private void 
  604. Zfree_cycle(Cycle *ptr)
  605. {
  606.     Cycle *ptr0, *ptr2;
  607.  
  608.         if (ptr == NULL)
  609.         return;
  610.         ptr0 = ptr;
  611.         do {
  612.         ptr2 = ptr->next;
  613.         Zfree((Free *)ptr);
  614.         ptr = ptr2;
  615.     } while (ptr != ptr0);
  616. }
  617.  
  618. private void 
  619. Zfree_font(Font *ptr)
  620. {
  621.     Font *ptr2;
  622.  
  623.         while (ptr != NULL) {
  624.         Zfree_cycle(ptr->cycle);
  625.         ptr2 = ptr->next;
  626.         Zfree((Free *)ptr);
  627.         ptr = ptr2;
  628.     }
  629. }
  630.  
  631.